//
package org.whyr.android.player;

import org.whyr.android.player.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class PlayerNotification{
	private Context context;
	private NotificationCompat.Builder mBuilder; 
	private NotificationManager notificationManager; 
	private Notification notification; 
	private String mContentText = "..."; 
	
	
	public PlayerNotification(Context c) {
		context = c; 
		init(); 

		notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	}
	public void init(){
		mBuilder = new NotificationCompat.Builder(context); 
		mBuilder.setSmallIcon(R.drawable.ic_play_notification); 
		mBuilder.setContentTitle("WHYR BR Community Radio"); 
		Intent intent = new Intent(context, MainActivity.class); 
		intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(contentIntent); 
	}
	private void finish(){
		Intent stopIntent = new Intent(context, PlayerService.class)
			.setAction("org.whyr.android.player.STOP");
		PendingIntent pendingStopIntent = PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.addAction(R.drawable.ic_stop_action, "stop", pendingStopIntent);
	}
	public void show(){
		mBuilder.setContentText(mContentText);
		notification = mBuilder.build();
		notification.flags = Notification.FLAG_ONGOING_EVENT; 
		notificationManager.notify(0, notification);
	}
	public void hide(){
		notificationManager.cancel(0); 
	}
	public void setText(String string){
		mContentText = string;  
		show(); 
	}
	public void play(){
		init(); 
		mBuilder.setSmallIcon(R.drawable.ic_play_notification); 
		Intent pauseIntent = new Intent(context, PlayerService.class)
			.setAction("org.whyr.android.player.PAUSE");
		PendingIntent pendingPauseIntent = PendingIntent.getService(context, 0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.addAction(R.drawable.ic_pause_action, "pause", pendingPauseIntent);
		finish(); 
		show(); 
		
	}
	public void pause(){
		init(); 
		mBuilder.setSmallIcon(R.drawable.ic_pause_notification);
		Intent playIntent = new Intent(context, PlayerService.class)
			.setAction("org.whyr.android.player.PLAY");
		PendingIntent pendingPlayIntent = PendingIntent.getService(context, 0, playIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.addAction(R.drawable.ic_play_action, "play", pendingPlayIntent);
		finish();
		show(); 
	}
}
